﻿using ASmile.Extern.Lib.Rsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ASmile.Extern.Lib.Test
{
    public class TestAsyncResponse : HttpResponseAsync
    {
        public override string Type => CType;

        static string CType { get; } = "t002";

        static bool IsRun { set; get; }

        void Run()
        {
            while (true)
            {
                SetData("当前时间", $"时间：{DateTime.Now} ，{Thread.CurrentThread.ManagedThreadId}");

                Thread.Sleep(5 * 1000);
            }
        }

        protected override void ReceiveJsonData(Dictionary<string, string> bodyData)
        {
            // 开始异步任务，可指定参数，
            // 需要判断是否运行，只有第一次需要运行，客户端在重复调用
            if (!IsRun)
            {
                IsRun = true;
                Task.Factory.StartNew(Run);
                Thread.Sleep(5 * 1000);
            }
        }
    }
}
